package com.z;

import com.z.relam.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @ProjectName: springboot-08-shiro
 * @Package: PACKAGE_NAME
 * @ClassName: TestCustomerAuthorization
 * @author: Arisapoke
 * @date: 2021/2/2 17:28
 * @Version: 1.0
 */
public class TestCustomerAuthorization {

    //自定义realm
    public static void main(String[] args) {
        //new DefualtSecurityManager
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        //设置 realm
        defaultSecurityManager.setRealm(new CustomerRealm());
        //使用管理工具设置安全管理器
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //通过安全工具类获取SecurityManager
        Subject subject = SecurityUtils.getSubject();
        //创建token
        UsernamePasswordToken token = new UsernamePasswordToken("chenhui", "123456");
        //验证
        try {
            subject.login(token);
        }catch (AuthorizationException e){
            e.printStackTrace();
        }
    }
}
